package conson.tech.manage.activity.service.impl;

import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import conson.tech.manage.activity.entity.dto.BathBeachTicketDTO;
import conson.tech.manage.activity.entity.pojo.TicketInfoRecord;
import conson.tech.manage.activity.mapper.TicketRecordMapper;
import conson.tech.manage.activity.service.CouPonCallService;
import conson.tech.manage.activity.service.MessagePushService;
import conson.tech.manage.activity.service.TicketRecordService;
import conson.tech.manage.api.activity.constant.ActivityConstant;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * ClassName: TicketRecordServiceImpl
 * Description:
 * Author: HXL
 * Date: 2020/5/22 2:03 下午
 * Version: 1.0
 */
@Slf4j
@Service
public class TicketRecordServiceImpl implements TicketRecordService {
    @Autowired
    private TicketRecordMapper ticketRecordMapper;
    @Autowired
    private CouPonCallService couPonCallService;
    @Autowired
    private MessagePushService messagePushService;
    /**
     * Title: 根据票券记录查询票券信息
     * Description:
     * @param record
     * @return: java.util.List<conson.tech.manage.activity.entity.pojo.TicketInfoRecord>
     * Company: GX
     * Author HXL
     * Date 2020/5/26 9:20 上午
     */
    @Override
    public List<TicketInfoRecord> queryTicketInfoRecord(TicketInfoRecord record) {

        return ticketRecordMapper.queryTicketInfoRecord(record);
    }
    /**
     * Title: 领取票券
     * Description:
     * @param userId
     * @param mobile
     * @param list
     * @return: int
     * Company: GX
     * Author HXL
     * Date 2020/5/26 9:21 上午
     */
    @Transactional
    @Override
    public int receiveTicketInfo(Long userId,String mobile,List<TicketInfoRecord> list) {
        int i = 0;
        if (!CollectionUtils.isEmpty(list)){
            List<TicketInfoRecord> recordList = new ArrayList<>();
            for (TicketInfoRecord record: list){
                try {
                record.setUserId(userId);
                record.setUserMobile(mobile);
                record.setDelFlag(1);
                record.setTicketStatus(1);
                record.setCreatedTime(LocalDateTime.now());
                record.setUpdatedTime(LocalDateTime.now());
                QueryWrapper queryWrapper = new QueryWrapper();
                TicketInfoRecord queryRecord = new TicketInfoRecord();
                queryRecord.setTicketId(record.getTicketId());
                queryRecord.setUserId(userId);
                queryRecord.setUserMobile(mobile);
                queryWrapper.setEntity(queryRecord);
                TicketInfoRecord ticketInfoRecord = ticketRecordMapper.selectOne(queryWrapper);
                if (ticketInfoRecord != null){
                    log.info("【票券领取】-> 根据参数:[{}],查询数据库发现已经领取过了。", JSONUtil.toJsonStr(queryRecord));
                    continue;
                }
                    //  票券类型：隧e通通行券: 1 公交充值优惠券:2 停车场优惠券:3 海水浴场免费更衣券:4
                    if (record.getTicketType() == ActivityConstant.TICKET_TYPE_ONE){
                        //业务系统票券ID
                     record.setBusTicketId(couPonCallService.EPassGrantTicket(mobile));
                    }//公交
//                    else if (record.getTicketType() == ActivityConstant.TICKET_TYPE_TWO){
//                        CouponFeginForm feginForm = new CouponFeginForm();
//                        feginForm.setUserId(String.valueOf(userId));
//                        String couponIndentify =  couPonCallService.transitRechargeGrantTicket(feginForm);
//                        record.setBusTicketId(couponIndentify);
//                    }//停车场
                    else if(record.getTicketType() == ActivityConstant.TICKET_TYPE_THREE){
                        record.setBusTicketId(couPonCallService.ParkingLotTicket(mobile));
                    }  //博物馆
                    else if (record.getTicketType() == ActivityConstant.TICKET_TYPE_FIVE){
                        record.setBusTicketId(couPonCallService.museumTicket(userId,mobile,record.getTicketStartTime(),record.getTicketEndTime()));
                    }
                    recordList.add(record);
                } catch (Exception e) {
                   log.error("【票券领取】业务系统领取票券出错:",e);
                }
               // i += ticketRecordMapper.insert(record);
            }
            for (TicketInfoRecord record : recordList) {
                i += ticketRecordMapper.insert(record);
            }
        }
        return i;
    }
    /**
     * Title: 修改票券状态
     * Description:
     * @param busTicketId
     * @param status
     * @return: int
     * Company: GX
     * Author HXL
     * Date 2020/5/26 9:21 上午
     */
    @Transactional
    @Override
    public int updateTicketRecord(String busTicketId, String status) {
        return  ticketRecordMapper.updateTicketRecord(busTicketId,Integer.valueOf(status));
    }
    /**
     * Title: 激活海水浴场票券
     * Description:
     * @param dto
     * @return: int
     * Company: GX
     * Author HXL
     * Date 2020/5/26 9:21 上午
     */
    @Transactional
    @Override
    public int activationTicketInfo(BathBeachTicketDTO dto) {
        String orderId = couPonCallService.activationBathBeachTicket(dto);
        TicketInfoRecord record = new TicketInfoRecord();
        record.setId(dto.getTicketRecordId());
        record.setTicketStatus(4);
        record.setBusTicketId(orderId);
        record.setTicketStartTime(new Date());
        Calendar c = Calendar.getInstance();
        c.set(Calendar.HOUR_OF_DAY, 23);
        c.set(Calendar.MINUTE, 59);
        c.set(Calendar.SECOND, 59);
        c.set(Calendar.MILLISECOND, 0);
        record.setTicketEndTime(c.getTime());
        record.setUpdatedTime(LocalDateTime.now());
        int i = ticketRecordMapper.updateById(record);
        if (i >0 && !StringUtils.isEmpty(orderId)){
            messagePushService.pushMessage(orderId);
        }
        return i;
    }
    /**
     * Title: 检查当前时间是否已经激活过一张海水浴场更衣券
     * Description:
     * @param
     * @return: int
     * Company: GX
     * Author HXL
     * Date 2020/5/26 10:49 上午
     */
    @Override
    public int checkActivationTicket(String mobile) {
        Integer integer = ticketRecordMapper.checkActivationTicket(mobile);
        if (integer == null){
            integer = 0;
        }
        return integer;
    }

    @Override
    public TicketInfoRecord checkTermOfValidity(TicketInfoRecord record) {
        return  ticketRecordMapper.checkTermOfValidity(record);
    }

    @Override
    public TicketInfoRecord queryTicketInfoRecordByBusTicketId(String busTicketId) {
        TicketInfoRecord record = new TicketInfoRecord();
        record.setBusTicketId(busTicketId);
        record.setDelFlag(1);
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.setEntity(record);

        return ticketRecordMapper.selectOne(queryWrapper);
    }

    @Override
    public TicketInfoRecord queryTicketInfoRecordById(Long id) {
        return ticketRecordMapper.selectById(id);
    }

    @Override
    public List<TicketInfoRecord> queryTicketInfoRecordListById(List<Long> ticketRecordList) {
        return ticketRecordMapper.queryTicketInfoRecordListById(ticketRecordList);
    }
    /**
     * Title: 根据ID集合处理已经过期的票券
     * Description:
     * @param idList
     * @return: int
     * Company: GX
     * Author HXL
     * Date 2020/6/1 6:28 下午
     */
    @Override
    public int updateTimeOutTicketRecordList(List<Long> idList) {

        return ticketRecordMapper.updateTimeOutTicketRecordList(idList);
    }
    /**
     * Title: 查询当前时间所有过期的票券的ID
     * Description:
     * @param
     * @return: java.util.List<java.lang.Long>
     * Company: GX
     * Author HXL
     * Date 2020/6/1 6:34 下午
     */
    @Override
    public List<Long> queryTimeOutTicket() {
        return ticketRecordMapper.queryTimeOutTicket();
    }
}
